using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Grenade3D : EnemyBaseFixed
{
    private SeqAction workAction=new(){intervalTime=8f/60f};
    private Sprite[] rollSprites;
    private Sprite[] smallRollSprites;

    void Awake()
    {
        base.Init();
        rollSprites=new Sprite[]{
            ResourcesPool.Sprites["魂-第二关-雷1"],
            ResourcesPool.Sprites["魂-第二关-雷2"],
            ResourcesPool.Sprites["魂-第二关-雷3"],
            ResourcesPool.Sprites["魂-第二关-雷4"],
            ResourcesPool.Sprites["魂-第二关-雷5"],
        };
        smallRollSprites=new Sprite[]{
            ResourcesPool.Sprites["魂-第二关-雷-小1"],
            ResourcesPool.Sprites["魂-第二关-雷-小2"],
            ResourcesPool.Sprites["魂-第二关-雷-小3"],
        };
    }
    void Start()
    {
        rig.gravityScale=0f;
        rig.velocity=Vector2.zero;
        Run();
    }

    void FixedUpdate()
    {
        base.CommonFixedUpdate();
    }
    protected void UpdateStateNone(bool stateChanged, EnumState inState, out EnumState outState){
        outState=EnumState.Working;
    }
    protected void UpdateStateWorking(bool stateChanged, EnumState inState, out EnumState outState){
        outState=inState;
        if(stateChanged){
            rig.gravityScale=1f;
            rig.velocity=Vector2.up*5f;
        }
        workAction.Update((SeqAction seq)=>{
            Sprite[] sprites=rollSprites;
            if(rig.velocity.y>0){
                sprites=smallRollSprites;
            }
            int index=seq.seqNo%sprites.Length;
            mainSR.sprite=sprites[index];
        });

        RaycastHit2D hit=Physics2D.Raycast(transform.position+Vector3.down*0.5f,Vector2.down,0.1f,groundLayer);
        if(hit){
            outState=EnumState.Die;
        }
    }

    protected override void UpdateStateDie(bool stateChanged, EnumState inState, out EnumState outState)
    {
        base.UpdateStateDie(stateChanged, inState, out outState);
        rig.velocity=Vector2.zero;
        rig.gravityScale=0f;
        Instantiate(Constant.Prefabs.EnemyBoom2.Load(),transform.position,Quaternion.identity);
        Constant.Sounds.EnemyDeath2.PlayOneShot();
        Destroy(gameObject);
    }


}
